
/* Generated by Interface Builder */

#import "Distributer.h"
#import "Clock.h"

#import <appkit/Window.h>
#import <appkit/Application.h>
#import <appkit/Button.h>
#import <appkit/Matrix.h>
#import <defaults/defaults.h>
#import <appkit/OpenPanel.h>
#import <stdlib.h>
#import <string.h>

#define SECONDS 0
#define MILITARY 1
#define CHIME 2
#define BUTTON 3
#define SET 4
#define BEZIER 5

#define DEFNAME "DockClock"

static NXDefaultsVector ClockDefaults = {
    {"Type", "1"},
    {"Seconds", "0"},
    {"Military", "0"},
    {"Chime", "0"},
    {"ChimeFile",""},
    {"Bezier","0"},
    {NULL}
};

char chimeFile[255];

@implementation Distributer

- showPreferences:sender
{
    if (!PreferencePanel) {
	[NXApp loadNibSection:"Preferences.nib" owner:self withNames:NO];
    }
    [PreferencePanel makeKeyAndOrderFront:sender];

    if (!buttonView) {
	buttonView = [[Clock alloc] init];
	[TypeButton addSubview:buttonView];
    }
    [buttonView setType:[iconView getType]];
    [buttonView setSeconds:[iconView getSeconds]];
    [buttonView setMilitary:[iconView getMilitary]];
    [buttonView setChime:[iconView getChime]];
    [buttonView setBezier:[iconView getBezier]];
    [buttonView setUpIcon];
    [buttonView  display];
    if ([iconView getSeconds]) [SwitchMatrix selectCellWithTag:SECONDS];
    if ([iconView getMilitary]) [SwitchMatrix selectCellWithTag:MILITARY];
    if ([iconView getChime]) [SwitchMatrix selectCellWithTag:CHIME];
    if ([iconView getBezier]) [SwitchMatrix selectCellWithTag:BEZIER];
    [chimeFileField setStringValue:chimeFile];

    return self;
}

- appDidInit:sender
{
    int Cflag;
    char *chimeTmp;

    iconView = [[Clock alloc] init];
    [[NXApp appIcon] setContentView:iconView];

    // Get everything from the defaults database
    NXRegisterDefaults(DEFNAME,ClockDefaults);

    [iconView setType:atof(NXGetDefaultValue(DEFNAME,"Type"))];
    [iconView setSeconds:atof(NXGetDefaultValue(DEFNAME,"Seconds"))];
    [iconView setBezier:atof(NXGetDefaultValue(DEFNAME,"Bezier"))];
    [iconView setMilitary:atof(NXGetDefaultValue(DEFNAME,"Military"))];

    chimeTmp = (char *) NXGetDefaultValue(DEFNAME,"ChimeFile");
    strcpy(chimeFile,chimeTmp);	    

    if ((Cflag = atof(NXGetDefaultValue(DEFNAME,"Chime"))) == 1) {
	if (chimeFile[0] == '\0') [iconView setChime:0];
	else {
	    [iconView setChime:1];
	    [iconView setChimeFile:chimeFile];
	}
    }

    [iconView display];

    return self;
}

- showInfo:sender
{
    if (!InfoPanel) {
	[NXApp loadNibSection:"Info.nib" owner:self withNames:NO];
    }
    [InfoPanel makeKeyAndOrderFront:sender];

    return self;
}

- changePref:sender
{
    int Cflag;
    int sw;
    id OP;
    char tmp[4];
    const char *ftypes[2] = {"snd",NULL};

    sw = [[sender selectedCell] tag];
    switch(sw) {
	case SECONDS:
	    [buttonView setSeconds:[[sender findCellWithTag:sw] state]];
	    sprintf(tmp,"%d",[buttonView getSeconds]);
	    NXWriteDefault(DEFNAME,"Seconds",tmp);
	    break;
	case MILITARY:
	    [buttonView setMilitary:[[sender findCellWithTag:sw] state]];
	    sprintf(tmp,"%d",[buttonView getMilitary]);
	    NXWriteDefault(DEFNAME,"Military",tmp);
	    break;
	case BEZIER:
	    [buttonView setBezier:[[sender findCellWithTag:sw] state]];
	    sprintf(tmp,"%d",[buttonView getMilitary]);
	    NXWriteDefault(DEFNAME,"Bezier",tmp);
	    break;
	case CHIME:
	    Cflag = [[sender findCellWithTag:sw] state];
	    if (Cflag && (*chimeFile == '\0')) {
		NXRunAlertPanel("Alert","No chime sound file has been selected.  Please select one and choose this option again.",
					NULL,NULL,NULL);
		[buttonView setChime:0];
		[[sender findCellWithTag:CHIME] setState:0];
		break;
	    }
	    if (Cflag)
		[buttonView setChimeFile:chimeFile];
	    sprintf(tmp,"%d",Cflag);
	    NXWriteDefault(DEFNAME,"Chime",tmp);
	    break;
	case BUTTON:
	    [buttonView setType:[buttonView getType]+1];
	    [buttonView setSeconds:atof(NXGetDefaultValue(DEFNAME,"Seconds"))];
	    break;
	case SET:
	    OP = [OpenPanel new];
	    if ([OP runModalForTypes:ftypes]) {
		sprintf(chimeFile,"%s/%s",[OP directory],*[OP filenames]);
		[chimeFileField setStringValue:chimeFile];
		NXWriteDefault(DEFNAME,"ChimeFile",chimeFile);
		[buttonView setChimeFile:chimeFile];
	    }
	    break;
    }
    [buttonView display];   // Force a display
    return self;
}

- appDidHide:sender
{
    char tmp[4];

    // All of these if's are pretty dumb, but I don't really want to change
    // anything unless I know the preferences have changed.

    if (buttonView && ([buttonView getType] != [iconView getType])) {
	sprintf(tmp,"%d",[buttonView getType]);
	NXWriteDefault(DEFNAME,"Type",tmp);	
	[iconView setType:[buttonView getType]];
    }
    if (buttonView) {
	[iconView setSeconds:[buttonView getSeconds]];
	[iconView setMilitary:[buttonView getMilitary]];
	[iconView setChime:[buttonView getChime]];    
	[iconView setChimeFile:chimeFile];
	[iconView setBezier:[buttonView getBezier]];
    }

    [InfoPanel close];			    // These guys have the "free when closed" flag set in IB
    InfoPanel = [InfoPanel free];

    [PreferencePanel close];
    PreferencePanel = [PreferencePanel free];  

    buttonView = nil;	// the Preference panel may free this but I want it nil

    [iconView display];
    return self;
}

- appDidResignActive:sender
{	
    [self appDidHide:self];
    return self;
}

@end
